package com.doitedu.day04.funcs

/**
 * @Date 22.6.23
 * @Created by HANGGE
 * @Description
 *  reduce 聚合  默认的计算逻辑是从左到右 依次计算
 */
object _04Reduce {
  def main(args: Array[String]): Unit = {

    val  ls = List[Int](1,2,3,4,5,6)

    /**
     * x1 第一个元素  后续的依次接收计算结果
     * x2 第二个元素  后续的是 第 3  4  5  6
     * (1,2)  =>  1+2
     * (3,3)  => 6
     * (6,4)  => 10
     * (10,5) =>15
     * (15,6) =>21
     */
    val res = ls.reduce((x1, x2) => x1 + x2)
    println(res)
    /**
     * (1,2) => -1
     * (-1,3) => -1-3
     * ...
     */
    val res2 = ls.reduce((x1, x2) => x1 - x2)
    println(res2)

    val arr = Array[String]("java","hgy","sql","hgy","linux","hgy")

    //println(arr.mkString(":"))
    println(arr.reduce((x1, x2) => s"$x1-$x2"))
    println(arr.reduce((x1, x2) => x1 +" "+x2))


/*    val sum = ls.sum
    println(sum)*/


  }

}
